<template>
  <div class="port">
    <div style="display: flex;justify-content: center;">
      <div style="display: flex;width: 300px;margin-top: 20px">
        <a-input placeholder="输入端口号查询" v-model:value="SearchCon" @keyup.enter="SearchBtn"></a-input>
        <a-button style="margin-left: 15px" @click="SearchBtn">查询</a-button>
      </div>
    </div>
    <div style="margin-left: 10px;margin-right: 10px;margin-top: 15px">
      <a-table :dataSource="dataSource" :columns="columns" :scroll="{x:550,y:280}"/>
    </div>
  </div>
</template>

<script>
import {ref} from "vue";
import {message} from "ant-design-vue";

export default {
  name: "PortSearch",
  setup() {
    let SearchCon = ref('')
    let dataSource = ref([])
    let columns = ref([
      {
        title: "排序",
        dataIndex: 'key',
        key: 'key',
        width: 60
      },
      {
        title: "协议",
        dataIndex: 'agreement',
        key: 'agreement',
        width: 65
      }, {
        title: "本地地址",
        dataIndex: 'localAddress',
        key: 'localAddress'
      }, {
        title: "外部地址",
        dataIndex: 'remoteAddress',
        key: 'remoteAddress'
      }, {
        title: "状态",
        dataIndex: 'status',
        key: 'status'
      }, {
        title: "PID",
        dataIndex: 'pid',
        key: 'pid',
        width: 85
      }
    ])
    const SearchBtn = () => {
      dataSource.value = []
      window.go.main.App.SearchPort(SearchCon.value).then(res => {
        if (res.error !== '') {
          message.error(res.error)
          dataSource.value = []
        } else {
          let res_list = []
          for (let i = 0; i < res.data.length; i++) {
            let list = res.data[i].trim().split(" ")
            let item_list = []
            for (let i = 0; i < list.length; i++) {
              if (list[i] !== '') {
                item_list.push(list[i])
              }
            }
            if (item_list.length !== 0) {
              res_list.push(item_list)
            }
          }
          for (let i = 0; i < res_list.length; i++) {
            let obj = {}
            obj.key = i
            obj.agreement = res_list[i][0]
            obj.localAddress = res_list[i][1]
            obj.remoteAddress = res_list[i][2]
            obj.status = res_list[i][3]
            obj.pid = res_list[i][4]
            dataSource.value.push(obj)
          }
        }
      })
    }
    return {
      SearchCon,
      SearchBtn,
      columns,
      dataSource
    }
  }
}
</script>

<style scoped>

</style>